package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IDepartmentService departmentService;
	@Autowired
	private IPermissionService permissionService;

	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(Role role,Long[] ids){
		if(role.getId() != null){
			roleService.update(role,ids);
		}else {
			roleService.insert(role,ids);
		}
		return new JsonResult();
	}

	@RequestMapping("/input")
	public String input(Long id,Model model){
		if(id != null){
			model.addAttribute("role",roleService.get(id));
		}
		model.addAttribute("permissions",permissionService.selectAll());
		return "/role/input";
	}

	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(Long id){
		if(id != null){
			roleService.delete(id);
		}
		return new JsonResult();
	}

	@RequestMapping("/list")
	public String list(@ModelAttribute("qo")QueryObject qo, Model model){
		PageInfo pageInfo = roleService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		return "/role/list";
	}
}
